package com.easyfun.healthmagicbox.inputviews;

import java.sql.SQLException;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.util.SparseArray;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.SeekBar.OnSeekBarChangeListener;

import com.easyfun.healthmagicbox.ProfileActivity;
import com.easyfun.healthmagicbox.R;
import com.easyfun.healthmagicbox.SubLastProfileActivity;
import com.easyfun.healthmagicbox.base.BaseActivity;
import com.easyfun.healthmagicbox.base.DatabaseAccess;
import com.easyfun.healthmagicbox.base.DatabaseAccessImplementation;
import com.easyfun.healthmagicbox.comm.HealthInputDataUploadTask;
import com.easyfun.healthmagicbox.comm.HealthThreadPool;
import com.easyfun.healthmagicbox.inputviews.FitnessInputActivity.MyOnSeekBarChangeListener;
import com.easyfun.healthmagicbox.ormlite.DatabaseHelper;
import com.easyfun.healthmagicbox.ormlite.HealthInputTypeEnum;
import com.easyfun.healthmagicbox.pojo.ConstantData;
import com.easyfun.healthmagicbox.pojo.HealthInputData;
import com.easyfun.healthmagicbox.sync.BasicCommCallback;
import com.easyfun.healthmagicbox.util.HealthDateUtil;
import com.easyfun.healthmagicbox.widget.DateTimeOnTouchListener;
import com.easyfun.healthmagicbox.widget.SeekBarWithAdjust;
import com.j256.ormlite.dao.Dao;

public class PhysicalExamInputActivity extends BaseActivity implements DatabaseAccess {
	private String LOG_TAG=PhysicalExamInputActivity.class.toString();
	private SparseArray<String> changedValues = new SparseArray<String>();
	SeekBarWithAdjust seekbarweight;
	TextView txtweight;
	
	SeekBarWithAdjust seekbarheigth;
	TextView txtheigth;
	
	SeekBarWithAdjust seekbarxueya_gaoya;
	TextView txtxueya_gaoya;
	
	SeekBarWithAdjust seekbarxueya_diya;
	TextView txtxueya_diya;
	
	SeekBarWithAdjust seekbarxuetang_canqian;
	TextView txtxuetang_canqian;
	
	SeekBarWithAdjust seekbarxuetang_canhou;
	TextView txtxuetang_canhou;
	
	SeekBarWithAdjust seekbarxueyang;
	TextView txtxueyang;
	
	SeekBarWithAdjust seekbarxinlv;
	TextView txtxinlv;
	
	SeekBarWithAdjust seekbartiwen;
	TextView txttiwen;
	private EditText inputdatetime;
	
	
	//private SparseArray<String> changedValues = new SparseArray<String>();
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.inputphysicalexam);
		//should after set Content
		addbackButton(this);
		//personal ID
		if (!checkPersonId()) return;
		
		Button butSave = (Button) findViewById(R.id.butSavePhysical);
		butSave.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				saveExamData();
				Toast.makeText(PhysicalExamInputActivity.this, "保存", Toast.LENGTH_LONG).show();
				
				finish();
			}
		});
		seekbarweight = (SeekBarWithAdjust) findViewById(R.id.seekbarweight);
		txtweight = (TextView) findViewById(R.id.txtweight);
		
		seekbarheigth = (SeekBarWithAdjust) findViewById(R.id.seekbarheigth);
		txtheigth = (TextView) findViewById(R.id.txtheigth);		

		seekbarxueya_gaoya = (SeekBarWithAdjust) findViewById(R.id.seekbarxueya_gaoya);
		txtxueya_gaoya = (TextView) findViewById(R.id.txtxueya_gaoya);		

		seekbarxueya_diya = (SeekBarWithAdjust) findViewById(R.id.seekbarxueya_diya);
		txtxueya_diya = (TextView) findViewById(R.id.txtxueya_diya);	

		seekbarxuetang_canqian = (SeekBarWithAdjust) findViewById(R.id.seekbarxuetang_canqian);
		txtxuetang_canqian = (TextView) findViewById(R.id.txtxuetang_canqian);	

		seekbarxuetang_canhou = (SeekBarWithAdjust) findViewById(R.id.seekbarxuetang_canhou);
		txtxuetang_canhou = (TextView) findViewById(R.id.txtxuetang_canhou);	

		seekbarxueyang = (SeekBarWithAdjust) findViewById(R.id.seekbarxueyang);
		txtxueyang = (TextView) findViewById(R.id.txtxueyang);	

		seekbarxinlv = (SeekBarWithAdjust) findViewById(R.id.seekbarxinlv);
		txtxinlv = (TextView) findViewById(R.id.txtxinlv);	

		seekbartiwen = (SeekBarWithAdjust) findViewById(R.id.seekbartiwen);
		txttiwen = (TextView) findViewById(R.id.txttiwen);	
		
		inputdatetime = (EditText) findViewById(R.id.datetimeedit);
		inputdatetime.setText(HealthDateUtil.getCurrentDateStr("yyyy-MM-dd HH:mm"));
		inputdatetime.setOnTouchListener(
				new DateTimeOnTouchListener(this, inputdatetime));
		setupView();
		
	}
	@Override
	public DatabaseHelper getHelper(Context context) {
		return DatabaseAccessImplementation.getInstance().getHelper(this);
	}
	
	void saveExamData()
	{
		Dao<HealthInputData, Integer> inputDataDao;
		// get our dao
		try {
			inputDataDao = getHelper(this).getInputDataDao();
			Log.i(LOG_TAG, "get last input data.");
		} catch (SQLException e) {
			e.printStackTrace();
			return;
		}
		// key is the value type which is enum
		int key = 0; 
		for(int i = 0; i < changedValues.size(); i++) 
		{ 
			key = changedValues.keyAt(i); // get the object by the key. 
			String value = changedValues.get(key); 
			HealthInputData hid = new HealthInputData();
			hid.setPersonID(m_personid);
			long dt = HealthDateUtil.getDate(inputdatetime.getText().toString(),"yyyy-MM-dd HH:mm").getTime();
			hid.setValueTime(dt);// HealthDateUtil.getCurrentDateStr()
			hid.setValueType(key);
			hid.setValueData(value);
			
			try {
				inputDataDao.create(hid);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				changedValues.clear();
				return;
			}
			
		}

		HealthInputDataUploadTask task = new HealthInputDataUploadTask(
				this, 
				m_personid, 
				new BasicCommCallback());
		HealthThreadPool.getInstance().execute(task);
		
		changedValues.clear();

		//close myself equal to go back.
		finish();
		
	}
	
	void setupView()
	{
		Dao<HealthInputData, Integer> inputDataDao;
		
		// get our dao
		try {
			inputDataDao = getHelper(this).getInputDataDao();
			Log.i(LOG_TAG, "get last input data.");
		} catch (SQLException e) {
			e.printStackTrace();
			return;
		}
		//
		

		//-------------------------------------------------------------------------------
		seekbarweight.setMax(150);
		seekbarweight.setProgress(15);
		txtweight.setText(String.valueOf(seekbarweight.getProgress())+this.getString(R.string.tizhong_gongjin));
		seekbarweight.setOnSeekBarChangeListener(new MyOnSeekBarChangeListener(
				HealthInputTypeEnum.TIZHONG) 
		);
		
		
		//-------------------------------------------------------------------------------
		seekbarheigth.setMax(160);
		seekbarheigth.setProgress(130);
		txtheigth.setText(String.valueOf(40 + seekbarheigth.getProgress())+this.getString(R.string.shengo_limi));

		seekbarheigth.setOnSeekBarChangeListener(new MyOnSeekBarChangeListener(
				HealthInputTypeEnum.SHENGAO) 
		);
		//-------------------------------------------------------------------------------
		seekbarxueya_diya.setMax(100);  //[50,150]
		seekbarxueya_diya.setProgress(20);
		txtxueya_diya.setText(String.valueOf(50 + seekbarxueya_diya.getProgress())+this.getString(R.string.xueya_danwei));

		seekbarxueya_diya.setOnSeekBarChangeListener(new MyOnSeekBarChangeListener(
				HealthInputTypeEnum.XUEYA_DIYA));
		
		//-------------------------------------------------------------------------------
		seekbarxueya_gaoya.setMax(100); //[80,180]
		seekbarxueya_gaoya.setProgress(20);
		txtxueya_gaoya.setText(String.valueOf(80 + seekbarxueya_gaoya.getProgress())+this.getString(R.string.xueya_danwei));

		seekbarxueya_gaoya.setOnSeekBarChangeListener(new MyOnSeekBarChangeListener(
				HealthInputTypeEnum.XUEYA_GAOYA));
		
		//-------------------------------------------------------------------------------
		seekbarxuetang_canqian.setMax(100); //[2 22]
		seekbarxuetang_canqian.setProgress(15);
		
		int beforedot = (20 + seekbarxuetang_canqian.getProgress() *20/10) / 10;
		int afterdot = (20 + seekbarxuetang_canqian.getProgress() *20/10) % 10;
		String value =  
				String.valueOf(beforedot)+"."+String.valueOf(afterdot);
		txtxuetang_canqian.setText(value
						+this.getString(R.string.xuetang_danwei));

		seekbarxuetang_canqian.setOnSeekBarChangeListener(new MyOnSeekBarChangeListener(
				HealthInputTypeEnum.XUETANG_CANQIAN));
		
		//-------------------------------------------------------------------------------
		seekbarxuetang_canhou.setMax(100);//[2 22]
		seekbarxuetang_canhou.setProgress(15);
		
		beforedot = (20 + seekbarxuetang_canhou.getProgress() *20/10) / 10;
		afterdot = (20 + seekbarxuetang_canhou.getProgress() *20/10) % 10;
		value =  
				String.valueOf(beforedot)+"."+String.valueOf(afterdot);
		txtxuetang_canhou.setText(value
						+this.getString(R.string.xuetang_danwei));

		seekbarxuetang_canhou.setOnSeekBarChangeListener(new MyOnSeekBarChangeListener(
				HealthInputTypeEnum.XUETANG_CANHOU));
		
		//-------------------------------------------------------------------------------
		seekbarxueyang.setMax(100);
		seekbarxueyang.setProgress(100);
		txtxueyang.setText(String.valueOf(seekbarxueyang.getProgress())+"% ");

		seekbarxueyang.setOnSeekBarChangeListener(new MyOnSeekBarChangeListener(
				HealthInputTypeEnum.XUEYANG));
		
		//-------------------------------------------------------------------------------
		seekbarxinlv.setMax(200);
		seekbarxinlv.setProgress(100);
		txtxinlv.setText(String.valueOf(seekbarxinlv.getProgress())+this.getString(R.string.xilv_danwei));

		seekbarxinlv.setOnSeekBarChangeListener(new MyOnSeekBarChangeListener(
				HealthInputTypeEnum.XINLV));
		
		//-------------------------------------------------------------------------------
		seekbartiwen.setMax(100); //[34,44] diff 10
		seekbartiwen.setProgress(15);
		

		seekbartiwen.setOnSeekBarChangeListener(new MyOnSeekBarChangeListener(
				HealthInputTypeEnum.TIWEN));
		
		beforedot = (350 + seekbartiwen.getProgress() /10) / 10;
		afterdot = (350 + seekbartiwen.getProgress() /10) % 10;
		value =  
				String.valueOf(beforedot)+"."+String.valueOf(afterdot);
		txttiwen.setText(value
						+this.getString(R.string.tiwen_danwei));
		
	}
	

	class MyOnSeekBarChangeListener implements OnSeekBarChangeListener
	{
		private HealthInputTypeEnum mtype;
		public MyOnSeekBarChangeListener(HealthInputTypeEnum type)
		{
			mtype = type;
		}
		@Override
		public void onProgressChanged(SeekBar seekBar, int progress,
				boolean fromUser) {
			// TODO Auto-generated method stub

			
			switch (mtype)
			{
			case TIZHONG:
				changedValues.append(mtype.toInteger(), 
						String.valueOf(seekBar.getProgress() ));
				txtweight.setText(String.valueOf(seekBar.getProgress())+
				PhysicalExamInputActivity.this.getString(R.string.tizhong_gongjin));
				break;
			case SHENGAO:
				changedValues.append(mtype.toInteger(), 
						String.valueOf(seekBar.getProgress() + 40));
				txtheigth.setText(String.valueOf(seekBar.getProgress()+ 40)+
				PhysicalExamInputActivity.this.getString(R.string.shengo_limi));
				break;
			case XUEYA_DIYA:
				changedValues.append(mtype.toInteger(), 
						String.valueOf(50 + seekBar.getProgress()));
				txtxueya_diya.setText(String.valueOf(50 + seekBar.getProgress())+
				PhysicalExamInputActivity.this.getString(R.string.xueya_danwei));
				break;
			case XUEYA_GAOYA:
				changedValues.append(mtype.toInteger(), 
						String.valueOf(80 + seekBar.getProgress()));
				txtxueya_gaoya.setText(String.valueOf(80 + seekBar.getProgress())+
				PhysicalExamInputActivity.this.getString(R.string.xueya_danwei));
				break;
			case XUETANG_CANQIAN:
				int beforedot = (20 + seekbarxuetang_canqian.getProgress() *20/10) / 10;
				int afterdot = (20 + seekbarxuetang_canqian.getProgress() *20/10) % 10;
				String value =String.valueOf(beforedot)+"."+String.valueOf(afterdot);
				
				changedValues.append(mtype.toInteger(), 
						value);
				txtxuetang_canqian.setText(value+
				PhysicalExamInputActivity.this.getString(R.string.xuetang_danwei));
				break;
			case XUETANG_CANHOU:
				beforedot = (20 + seekbarxuetang_canhou.getProgress() *20/10) / 10;
				afterdot = (20 + seekbarxuetang_canhou.getProgress() *20/10) % 10;
				value =String.valueOf(beforedot)+"."+String.valueOf(afterdot);
				
				changedValues.append(mtype.toInteger(), 
						value);
				txtxuetang_canhou.setText(value+
				PhysicalExamInputActivity.this.getString(R.string.xuetang_danwei));
				break;
			case XINLV:
				changedValues.append(mtype.toInteger(), 
						String.valueOf(seekBar.getProgress()));
				txtxinlv.setText(String.valueOf(seekBar.getProgress())+
				PhysicalExamInputActivity.this.getString(R.string.xilv_danwei));
				break;
			case TIWEN:
				beforedot = (350 + seekBar.getProgress() ) / 10;
				afterdot = (350 + seekBar.getProgress() ) % 10;
				value = String.valueOf(beforedot)+"."+String.valueOf(afterdot);
				
				changedValues.append(mtype.toInteger(),value);
				txttiwen.setText(value+
				PhysicalExamInputActivity.this.getString(R.string.tiwen_danwei));
				break;
			case XUEYANG:
				changedValues.append(mtype.toInteger(), 
						String.valueOf(seekBar.getProgress() ));
				txtxueyang.setText(String.valueOf(seekBar.getProgress())+ "%");
				break;
			default:
				break;	
			}
		}

		@Override
		public void onStartTrackingTouch(SeekBar seekBar) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void onStopTrackingTouch(SeekBar seekBar) {
			// TODO Auto-generated method stub
			
		}
		
	}
}
